/**
 * Created by qb181 on 2017/11/1.
 */

//利用匿名函数将脚本包裹起来
(function() {

  //自定义命名空间
  var homePage = {};

  homePage.waveEffect = function(){
    homePage.wave = document.getElementById("wave");
    homePage.canvas = document.createElement('canvas');
    homePage.ctx = homePage.canvas.getContext('2d');
    homePage.canvas.width = wave.offsetWidth;
    homePage.canvas.height = wave.offsetHeight;
    homePage.wave.appendChild(homePage.canvas);
    homePage.animate();
    homePage.r = parseFloat(document.getElementById("val").innerHTML) / 100;
}
    homePage.animate = function() {
      var requestAnimationFrame =
        window.requestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (callback) {
          window.setTimeout(callbacks, 1000 / 60);
        };


      homePage.loop();
    }
  homePage.step = 0;
  homePage.loop = function() {
    homePage.step += 2;
    var changeHeight = 8;
    if (homePage.r == 1 || homePage.r == 0) {
      changeHeight = 0;
    }
    var height = homePage.canvas.height - homePage.canvas.height * homePage.r;
    var angle = homePage.step * Math.PI / 180;
    var deltaHeight = Math.cos(angle) * changeHeight;
    var deltaHeightRight = Math.sin(angle) * changeHeight;
    homePage.ctx.clearRect(0, 0, homePage.canvas.width, homePage.canvas.height);
    homePage.ctx.beginPath();
    homePage.ctx.lineWidth = 0;
    homePage.ctx.fillStyle = "#87d6fe"
    homePage.ctx.moveTo(0, height + deltaHeight - changeHeight);
    homePage.ctx.bezierCurveTo(homePage.canvas.width / 2, height + deltaHeight, homePage.canvas.width / 2, height + deltaHeightRight, homePage.canvas.width, height + deltaHeightRight);
    homePage.ctx.lineTo(homePage.canvas.width, homePage.canvas.height);
    homePage.ctx.lineTo(0, homePage.canvas.height);
    homePage.ctx.lineTo(0, height + deltaHeight - changeHeight);
    homePage.ctx.closePath();
    homePage.ctx.fill();
    requestAnimationFrame(homePage.loop);
  }
  window.HomePageJS = homePage;
})();
